package com.glw.sso.platform.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.glw.sso.platform.common.util.LoginContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "createUser", () -> LoginContext.getCurrentLoginUsernameOrDefault("unknow"), String.class);
        this.strictInsertFill(metaObject, "createUserId", () -> LoginContext.getCurrentLoginUsernameOrDefault("unknow"), String.class);
        this.strictInsertFill(metaObject, "isDelete", () -> Boolean.FALSE, Boolean.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateUser", () -> LoginContext.getCurrentLoginUsernameOrDefault("unknow"), String.class);
        this.strictUpdateFill(metaObject, "updateUserId", () -> LoginContext.getCurrentLoginUsernameOrDefault("unknow"), String.class);
    }
}